package server;

import util.*;
import util.PropertyMgr;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class StartServer {

	/**
	 * @param args
	 */



	public static void main(String[] args) {
		Socket connectionsocket;
		ServerSocket serversocket = null;
		int port = 8089;
		
		ConnectionPoolManager connectionPool = null;

		Logger.ln("The simple httpserver");

		try {

			Logger.ln("Trying to bind to localhost on port " + Integer.toString(port)
					+ "...");
			serversocket = new ServerSocket(port);
		} catch (Exception e) {
			e.printStackTrace();
			Logger.ln("\nFatal Error:" + e.getMessage());
			return;
		}
		Logger.ln("OK!\n");
		
		PropertyMgr.loadProperty();
		
		connectionPool = new ConnectionPoolManager();
		
		//Logger.ln(PropertyMgr.getValue("default"));
		//Logger.ln(PropertyMgr.getValue(null));
		//Logger.ln(PropertyMgr.getValue("default123"));

		while (true) {
			Logger.ln("\nReady, Waiting for requests...\n\n\n\n");
			try {

				connectionsocket = serversocket.accept();

				InetAddress client = connectionsocket.getInetAddress();

				Logger.ln(client.getHostName() + " connected to server.\n");
				new RequestHandler(connectionsocket).start();

			} catch (Exception e) { // catch any errors, and print them
				Logger.ln("\nError(1):" + e.getMessage());
			}

		}

	}

}
